package com.yuju.controller.order;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuju.account.enums.DumpRoomAccountReturnCodeEnum;
import com.yuju.model.account.AccountModel;
import com.yuju.order.service.IRoomOrderService;
import com.yuju.account.service.IAccountService;
import com.yuju.common.Result;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.vo.account.AccountVO;

/**
 * 类名：DumpRoomAccountController  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2017年12月11日 下午3:51:12  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("DumpRoomAccount")
public class DumpRoomAccountController extends BaseController<AccountVO, AccountModel> {
	
	@Autowired
	private IAccountService accountService;
	@Autowired
	private IRoomOrderService roomOrderService;
	
	/**
	 * 功能：新增哑房账<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月11日 下午4:56:11 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj){
		JSONArray jsonArray = obj.getJSONArray("accounts");
		String jsonString = jsonArray.toJSONString();
		ArrayList<AccountVO> accounts = JSON.parseObject(jsonString, new TypeReference<ArrayList<AccountVO>>(){});
		
		String hotelId = obj.getString("hotelId");
		String hotelCode = obj.getString("hotelCode");
		String groupId = obj.getString("groupId");
		String empId = obj.getString("empId");
		String roomOrderId = obj.getString("roomOrderId");
		
		for (AccountVO accountVO : accounts) {
			accountVO.setHotelId(hotelId);
			accountVO.setHotelCode(hotelCode);
			accountVO.setGroupId(groupId);
			accountVO.setCreateUserId(empId);
		}
		String id = roomOrderService.saveRoomOrderForDump(roomOrderId,groupId,hotelId,hotelCode,empId,convert(accounts));
		Result result = new Result();
		if(id!=null){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(DumpRoomAccountReturnCodeEnum.DUMPROOMACCOUNT_SAVE_SUCCESS.getCode());
			result.setReturnMsg(DumpRoomAccountReturnCodeEnum.DUMPROOMACCOUNT_SAVE_SUCCESS.getDesc());
			result.setData(id);
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(DumpRoomAccountReturnCodeEnum.DUMPROOMACCOUNT_SAVE_FAILURE.getCode());
			result.setReturnMsg(DumpRoomAccountReturnCodeEnum.DUMPROOMACCOUNT_SAVE_FAILURE.getDesc());
		}
		return result;
		
	}
	
}
